<template>
  <div class="container">
    <div class="title">收货地址</div>
    <div class="main">
      <ul>
        <li @click="addAddress">
          <span class="iconfont icon-zengjiatianjiajiahao"></span>
          <p>添加新地址</p>
        </li>
        <li v-for="item in dataSource" :key="item.id">
          <p>姓名：{{ item.userName }}</p>
          <p>手机号：{{ item.phone }}</p>
          <p>地址：{{ item.address }}</p>
          <p>邮政编码：{{ item.postCode }}</p>
          <div class="btns">
            <el-button size="small" @click="editAddress(item)">编辑</el-button>
            <el-button size="small" @click="deleteAddress(item)"
              >删除</el-button
            >
          </div>
        </li>
      </ul>
    </div>
    <el-dialog v-model="isShowDialog" title="新增收货地址" width="30%">
      <el-form :model="form">
        <el-form-item label="收货人姓名：" label-width="120px">
          <el-input v-model="form.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="收货人电话：" label-width="120px">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收货人地址：" label-width="120px">
          <el-cascader v-model="form.address" :options="options" />
        </el-form-item>
        <el-form-item label="街道信息：" label-width="120px">
          <el-input v-model="form.street" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮政编码：" label-width="120px">
          <el-input v-model="form.postCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShowDialog = false">取消</el-button>
          <el-button type="primary" @click="isShowDialog = false"
            >确认</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import { computed, defineComponent, reactive, toRefs } from 'vue';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const data = require('@/mock/personal/my-address.json');

export default defineComponent({
  setup() {
    const state = reactive({
      isShowDialog: false,
      form: {
        userName: '',
        phone: '',
        address: '',
        street: '',
        postCode: '',
      },
    });
    const dataSource = computed(() => {
      return data.data;
    });
    const options = [
      {
        value: 'beijin',
        label: '北京',
        children: [
          {
            value: 'dongcheng',
            label: '东城区',
          },
          {
            value: 'xicheng',
            label: '西城区',
          },
        ],
      },
      {
        value: 'sichuan',
        label: '四川',
        children: [
          {
            value: 'suining',
            label: '遂宁',
          },
          {
            value: 'chengdu',
            label: '成都',
          },
        ],
      },
    ];
    return {
      dataSource,
      ...toRefs(state),
      options,
    };
  },
  methods: {
    addAddress() {
      this.isShowDialog = true;
    },
    editAddress(item: any) {
      this.form.userName = item.userName;
      this.form.phone = item.phone;
      this.form.street = item.address;
      this.form.postCode = item.postCode;
      this.isShowDialog = true;
    },
    deleteAddress(item: any) {
      console.log(item);
      ElMessageBox.confirm('确定要删除该收货地址?', '确认删除收货地址', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        ElMessage({
          type: 'success',
          message: '地址删除成功！',
        });
      });
    },
  },
});
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  max-height: 640px;
  border: 1px solid #ccc;
  padding: 62px 33px;
  overflow-y: scroll;

  .title {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    padding-bottom: 20px;
  }

  .main {
    ul {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      li {
        width: 270px;
        height: 176px;
        border: 1px solid #ddd;
        margin-top: 20px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 38px 22px;
        padding-top: 46px;
        box-sizing: border-box;
        position: relative;

        &:first-child {
          text-align: center;
          color: #666;
          cursor: pointer;
          span {
            font-size: 46px;
          }
          p {
            color: 16px;
          }
        }

        .btns {
          position: absolute;
          top: 10px;
          right: 10px;
        }
      }
    }
  }
}
::v-deep .el-cascader {
  width: 100%;
}
</style>
